<template>
  <el-row class="home" :gutter="20">
    <el-col :span="8" style="margin-top: 0px">
      <!--超级管理员卡片-->
      <el-card shadow="hover">
        <div class="user">
          <img src="../assets/images/user.png" alt=""/>
          <div class="user-info">
            <p class="name">Admin</p>
            <p class="role">超级管理员</p>
          </div>
        </div>
        <div class="login-info">
          <p>上次登录时间:<span>2023-9-12</span></p>
          <p>上次登录的地点:<span>北京</span></p>
        </div>
      </el-card>

      <!--售卖表格卡片-->
      <el-card shadow="hover" style="margin-top: 20px" height="450px">
        <el-table :data="tableData">
          <el-table-column
              v-for="(val, key) in tableLabel"
              :key="key"
              :prop="key"
              :label="val"
          >
          </el-table-column>
        </el-table>
      </el-card>
    </el-col>

    <el-col :span="16" style="margin-top: 0px" class="right-num">
      <!--订单数据卡片-->
      <div class="num">
        <el-card
            :body-style="{ display: 'flex', padding: 0 }"
            v-for="item in countData"
            :key="item.name"
        >
          <component
              class="icons"
              :is="item.icon"
              :style="{ background: item.color }"
          ></component>
          <div class="detail">
            <p class="num">￥{{ item.value }}</p>
            <p class="txt">{{ item.name }}</p>
          </div>
        </el-card>
      </div>

      <!--echart线形图卡片-->
      <el-card style="height: 220px">
        <div ref="echart" style="height: 220px"></div>
      </el-card>

      <div class="graph">
        <!--柱状图-->
        <el-card style="height: 240px">
          <div ref="userechart" style="height: 240px"></div>
        </el-card>
        <!--饼图-->
        <el-card style="height: 240px">
          <div ref="videoechart" style="height: 220px"></div>
        </el-card>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import {getCurrentInstance, onMounted, ref,reactive} from "vue";
import * as echarts from "echarts";

export default {
  name: "MainHome",
  setup() {
    // 售卖表格的标题
    const tableLabel = {
      name: "课程",
      todayBuy: "今日购买",
      monthBuy: "本月购买",
      totalBuy: "总购买",
    };

    const {proxy} = getCurrentInstance();

    // 售卖表格的数据
    let tableData = ref([]);
    //使用二次封装的axios向后端发起请求，请求被mock拦截，返回定义的后端数据
    //提供售卖表格的数据
    const getTableData = async () => {
      let res = await proxy.$api.getTableData();
      tableData.value = res.data.tableData
    }

    // 订单数据
    let countData = ref([]);
    //获取订单数据
    const getCountData = async () => {
      let res = await proxy.$api.getCountData();
      countData.value = res.data.countData
    }

    // 折线图绘制需要的配置
    let xOptions = reactive({
      // 图例文字颜色
      textStyle: {
        color: "#333",
      },
      grid: {
        left: "20%",
      },
      // 提示框
      tooltip: {
        trigger: "axis",
      },
      xAxis: {
        type: "category", // 类目轴
        data: [],
        axisLine: {
          lineStyle: {
            color: "#17b3a3",
          },
        },
        axisLabel: {
          interval: 0,
          color: "#333",
        },
      },
      yAxis: [
        {
          type: "value",
          axisLine: {
            lineStyle: {
              color: "#17b3a3",
            },
          },
        },
      ],
      color: ["#2ec7c9", "#b6a2de", "#5ab1ef", "#ffb980", "#d87a80", "#8d98b3"],
      series: [],
    });
    // 饼装图需要的配置
    let pieOptions = reactive({
      tooltip: {
        trigger: "item",
      },
      color: [
        "#0f78f4",
        "#dd536b",
        "#9462e5",
        "#a6a6a6",
        "#e1bb22",
        "#39c362",
        "#3ed1cf",
      ],
      series: [],
    });
    //折线图需要的数据
    let orderData = reactive({
      xData: [],
      series: [],
    });
    //柱状图需要的数据
    let userData = reactive({
      xData: [],
      series: [],
    });
    //饼状图需要的数据
    let videoData = reactive({
      series: [],
    });

    //获取折线图，柱状图，饼状图的数据
    const getEChartData = async () => {
      let result = await proxy.$api.getChartData();
      console.log(result,'result')
      let res = result.data.orderData;
      orderData.xData = res.date;
      const keyArray = Object.keys(res.data[0]);
      const series = [];
      keyArray.forEach((key) => {
        series.push({
          name: key,
          data: res.data.map((item) => item[key]),
          type: "line",
        });
      });
      orderData.series = series;
      xOptions.xAxis.data = orderData.xData;
      xOptions.series = orderData.series;
      // userData进行渲染
      let hEcharts = echarts.init(proxy.$refs["echart"]);
      hEcharts.setOption(xOptions);

      // 柱状图进行渲染的过程
      let userRes = result.data.userData;
      userData.xData = userRes.map((item) => item.date);
      userData.series = [
        {
          name: "新增用户",
          data: userRes.map((item) => item.new),
          type: "bar",
        },
        {
          name: "活跃用户",
          data: userRes.map((item) => item.active),
          type: "bar",
        },
      ];
      xOptions.xAxis.data = userData.xData;
      xOptions.series = userData.series;
      let uEcharts = echarts.init(proxy.$refs["userechart"]);
      uEcharts.setOption(xOptions);

      //  饼图进行渲染的过程
      let videoRes = result.data.videoData;
      videoData.series = [
        {
          data: videoRes,
          type: "pie",
        },
      ];
      pieOptions.series = videoData.series;
      let vEcharts = echarts.init(proxy.$refs["videoechart"]);
      vEcharts.setOption(pieOptions);
    };

    onMounted(() => {
      getTableData();
      getCountData();
      getEChartData();
    });

    return {
      tableData,
      tableLabel,
      countData,
    };
  },
}
</script>

<style lang="less" scoped>
.home {
  .user {
    display: flex;
    align-items: center;
    padding-bottom: 20px;
    border-bottom: 1px solid #ccc;
    margin-bottom: 20px;

    img {
      width: 150px;
      height: 150px;
      border-radius: 50%;
      margin-right: 40px;
    }
  }

  .login-info {
    p {
      line-height: 30px;
      font-size: 14px;
      color: #999;

      span {
        color: #666;
        margin-left: 60px;
      }
    }
  }

  .num {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;

    .el-card {
      width: 32%;
      margin-bottom: 20px;
    }

    .icons {
      width: 80px;
      height: 80px;
      font-size: 30px;
      text-align: center;
      line-height: 80px;
      color: #fff;
    }

    .detail {
      margin-left: 15px;
      display: flex;
      flex-direction: column;
      justify-content: center;

      .num {
        font-size: 30px;
        margin-bottom: 10px;
      }

      .txt {
        font-size: 14px;
        text-align: center;
        color: #999;
      }
    }
  }

  .graph {
    margin-top: 20px;
    display: flex;
    justify-content: space-between;

    .el-card {
      width: 48%;
    }
  }
}
</style>